import Egg from './egg';
class Ground{
    constructor(width,height,bgcolor,col,row){
        this.width = width;
        this.height = height;
        this.bgcolor = bgcolor;
        this.col = col;
        this.row = row;
        this.ground = null;
        this.egg = new Egg(col,row);
        this.pos = [];
    }
    //场地初始化
    init(){
        const cellWidth = this.width/this.row;
        const cellHeight = this.height/this.col;
        //草地
        const groundObj = document.createElement('div');
        groundObj.style.width = this.width+'px';
        groundObj.style.height = this.height+'px';
        groundObj.style.backgroundColor = this.bgcolor;
        groundObj.style.margin = '100px auto';
        groundObj.setAttribute('id','ground');
        //行
        const colObj = document.createElement('div');
        colObj.style.boxSizing = 'border-box';
        colObj.style.width = this.width+'px';
        colObj.style.height = cellHeight+'px';
        colObj.style.borderBottom = '1px solid #333';
        //列
        const rowObj = document.createElement('div');
        rowObj.style.boxSizing = 'border-box';
        rowObj.style.display = 'inline-block';
        rowObj.style.verticalAlign = 'top';
        rowObj.style.width = cellWidth+'px';
        rowObj.style.height = '100%';
        rowObj.style.borderRight = '1px solid #333';
        
        let colNum = this.col;
        let rowNum = this.row;
        do{
            let _colObj = colObj.cloneNode(true);
            do{
                let _rowObj = rowObj.cloneNode(true);
                if(rowNum===1){
                    _rowObj.style.borderRight = '';
                }
                _colObj.appendChild(_rowObj);
                _rowObj = null;
                rowNum--;
            }while(rowNum > 0);
            groundObj.appendChild(_colObj);
            if(colNum===1){
                _colObj.style.borderBottom = '';
            }
            _colObj = null;
            rowNum = this.row;
            colNum--;
        }while(colNum > 0);
        this.ground = groundObj;
        document.body.appendChild(groundObj);
    }
}

export default Ground;